<?php

namespace backend\services\base;

use backend\extensions\ApiBaseService;
use backend\models\base\BaseSaleGroup;
use backend\models\base\BaseStore;

/**
 * Desc 门店管理服务类
 * @author WMX
 */
class BaseSaleGroupService extends ApiBaseService
{

    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params)
    {
        //检索条件
        $condition = ['AND'];
        if ($params['name']) {
            $condition[] = ['like', 'name', $params['name']??''];
        }
        $list = BaseSaleGroup::getDataAll($condition, $params['page'], $params['page_size']);
        $store_list = BaseStore::getIndexAll('id');
        foreach($list['list'] as &$item) {
            $item['store_name'] = $store_list[$item['store_id']]['name'] ?? '';
        }

        return $list;
    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function create($params)
    {
        if (BaseSaleGroup::create($params) == false) {
            return self::jsonError('保存失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function update($params)
    {
        $condition = ['id' => $params['id']];
        unset($params['id']);
        if (BaseSaleGroup::updateData($params, $condition) === false) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 删除
     * @param array $params 参数 
     * * */
    public static function delete($params)
    {
        $condition = ['id' => $params['id']];
        if (BaseSaleGroup::del($condition) == false) {
            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 详情
     * @param array $params 参数 
     * **/
    public static function detail($params)
    {
        $condition = ['id' => $params['id']];
        $data = BaseSaleGroup::getDataOne($condition, '', []);
        return self::jsonSuccess($data);
    }

}
